package query.select

import ast.statement.select.SqlSelectQuery
import ast.statement.select.SqlValuesSelect
import database.DBConnection
import database.DB
import dsl.Query
import util.toSqlString
import visitor.getExpr
import visitor.getQueryExpr
import java.sql.Connection

/**
 * values select语句dsl类
 * @property db DB 数据库类型
 * @property conn Connection? 数据库连接
 * @property isTransaction Boolean 是否是事务
 * @property dbConnection DBConnection? 基础查询类
 * @property valuesSelect SqlValuesSelect values select语法树
 */
class ValuesSelect(
    var db: DB = DB.MYSQL,
    override var conn: Connection? = null,
    override var isTransaction: Boolean = false,
    override var dbConnection: DBConnection? = null
) : SelectQueryImpl() {
    val valuesSelect = SqlValuesSelect()

    /**
     * 获取sql语法树
     * @return SqlSelectQuery sql语法树
     */
    override fun getSelect(): SqlSelectQuery = this.valuesSelect

    /**
     * 获取数据库类型
     * @return DB 数据库类型
     */
    override fun getDbType(): DB = this.db

    /**
     * 生成sql语句
     * @return String sql语句
     */
    override fun sql(): String = toSqlString(valuesSelect, db)

    /**
     * 添加一行数据
     * @param row List<Any?> 一行数据的值列表
     * @return ValuesSelect values查询dsl
     */
    fun addRow(row: List<Any?>): ValuesSelect {
        val addRow = row.map {
            when (it) {
                is Query -> getQueryExpr(it, this.db).expr
                else -> getExpr(it)
            }
        }

        valuesSelect.values.add(addRow)

        return this
    }
}